var express = require("express");
var fs = require('fs');
var http = require('http');
var $ = require('jquery');
var jade = require('jade');
var iconv = require('iconv-lite'); 
var BufferHelper = require('bufferhelper');

var app = express();
app.use(express.logger());
app.use(express.static('html'))
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

function httpget (url,encoding,callback) {
	http.get(url,function(res){
		var bufferHelper = new BufferHelper();
		res.on('data', function (chunk) {
			bufferHelper.concat(chunk);
		});
		res.on('end',function(){ 
			var data = "";
			if(encoding == "UTF-8"){
				data = bufferHelper.toBuffer().toString();
			}
			else{
				data = iconv.decode(bufferHelper.toBuffer(),encoding);
			}
			callback(data);
		});
	})
}

function getAllFiles(root) {
	  var result = [], files = fs.readdirSync(root)
	  files.forEach(function(file) {
		    var pathname = root+ "/" + file
		      , stat = fs.lstatSync(pathname)
		    if (stat === undefined) return
		 
		    // 不是文件夹就是文件
		    if (!stat.isDirectory()) {
			      result.push(pathname)
			    // 递归自身
			    } else {
				      result = result.concat(getAllFiles(pathname))
				    }
	  });
	  return result
}

app.get('/hotlist',function(req,res) {
	var json = JSON.parse(fs.readFileSync('cache/gamersky.hotlist.json', "utf-8"));
	var array = [];
	for( var prop in json){
		array.unshift({url:prop,title:json[prop]});
	}
	json = new Object;
	for (var i = 0; i < array.length; i++) {
		var obj = array[i];
		json[obj.url] = obj.title;
	};
	var obj = { lists:json }
	res.render('hotlist',obj);
});

app.get('/page',function(req,res) {
	var url = req.query.url;
	console.log(url);
	filename = "cache/gamersky/" + encodeURIComponent(url);
	var file = fs.readFileSync(filename, "utf-8");
	res.send(file);

});

app.get('/config',function(req,res) {
	var json = null;
	res.render('config',json);
});

function test_images (res) {
	var json = JSON.parse(fs.readFileSync('cache/json/test.json', "utf-8"));
	res.render('page',json);
}

function getJsonFromFile(filename){
	return JSON.parse(fs.readFileSync(filename, "utf-8"));
}

function parse (res,url) {
	console.log("parse");
	console.log(url);

	var domain = url.match(/http:\/\/[^\/]*\//);
	
	console.log("domain:" + domain);
	//TODO:优化为一次读取后缓存
	var catalog = getJsonFromFile("cache/catalog.json");
	console.log("catalog:" + catalog);
	var id = catalog[domain];
	console.log("id:" + id);
	//TODO:优化为一次读取后缓存
	var config_filename = "cache/{name}.config.json".replace(/{name}/,id);
	var config = getJsonFromFile(config_filename);
	console.log(config);

	var filename = "cache/" + config.name + "/%s.json";
	filename = filename.replace(/%s/,encodeURIComponent(url));
	console.log(filename);
	if(fs.existsSync(filename)){
		var obj = JSON.parse(fs.readFileSync(filename, "utf-8"));	
		console.log(obj);
		res.render('page',obj);
	}
	else{
		var obj = new Object;
		obj.title = "";
		obj.source = url;
		obj.images = [];


		function get_next_page (url) {
			httpget(url,config.encoding,function(data) {
				console.log("encoding:" + config.encoding);
				if("" == obj.title){
					obj.title = $(data).find(config.title_selector).text().replace(/(^\s*)|(\s*$)/g, "");
				}
				$(data).find(config.img_selector).each(function(i,item) {
					var src = $(item).attr("src");
					var relative_path = config.img_relative_path != undefined && config.img_relative_path;
					src = relative_path ? domain + src : src;
					console.log(src);
					obj.images.push(src);
				});
				var next_button = $(data).find(config.next_selector);
				if(next_button.length > 0){			//有下一页
					var next_url = next_button.attr('href');
					next_url = url.match(/.*\//) + next_url.match(/[^\/]*$/);
					console.log("next_url:\n" + next_url);
					get_next_page(next_url);
					console.log("obj:\n" + obj);
					res.render('page',obj);
				}
				else{		//无下一页
					console.log("page finished");
					console.log(filename);
					//TODO:须先创建目录
					fs.writeFileSync(filename, JSON.stringify(obj));
					var hotlist_filename = "cache/{name}.hotlist.json".replace(/{name}/,id);
					console.log("热门列表文件名：" + hotlist_filename);
					var hotlist = getJsonFromFile(hotlist_filename);
					console.log(obj.source);
					console.log(obj.title);
					hotlist[obj.source] = obj.title;
					fs.writeFileSync(hotlist_filename, JSON.stringify(hotlist));
				}
			});
		}
		get_next_page(url);
	}
}

app.get('/analyse',function(req,res) {
	var url = req.query.url;
	console.log(url);
	parse(res,url);
});

app.get('/:thing', function (req, res) {
	console.log("test thing");
	res.send(req.route);
});

var port = process.env.PORT || 3000;
app.listen(port, function() {
	console.log("Listening on " + port);
});
